package com.eastfair.home.service.impl;

import cn.hutool.core.bean.BeanUtil;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.annotation.annotation.cache.Cache;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.home.dao.ExhibitionManageContactMapper;
import com.eastfair.home.dto.ExhibitionManageContactDTO;
import com.eastfair.home.dto.ExhibitionManageContactModifyDTO;
import com.eastfair.home.dto.ExhibitionManageContactPageQuery;
import com.eastfair.home.entity.ExhibitionManageContact;
import com.eastfair.home.service.ExhibitionManageContactService;
import com.eastfair.home.vo.ExhibitionManageContactVO;
import com.eastfair.venueservice.consts.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 业务实现类
 * 展会联系人
 * </p>
 *
 * @author linan
 * @date 2022-10-22
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class ExhibitionManageContactServiceImpl extends SuperServiceImpl<ExhibitionManageContactMapper, ExhibitionManageContact> implements ExhibitionManageContactService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ExhibitionManageContact> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }


    @Override
    protected R<Boolean> handlerSave(ExhibitionManageContact model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }
    /**
     * 通用查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    public List<ExhibitionManageContact> list(ExhibitionManageContactDTO modelDTO, List<Long> ids) {
        return ((ExhibitionManageContactService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids      主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
            magic = @Magic(
                    key = "{" +
                            "{'isEnabled', #args[0]?.isEnabled}," +
                            "{'exhibitionManageId', #args[0]?.exhibitionManageId}," +
                            "{'contact', #args[0]?.contact}," +
                            "{'contactMobile', #args[0]?.contactMobile}," +
                            "{'email', #args[0]?.email}," +
                            "{'spaceNames', #args[0]?.spaceNames}," +
                            "{'id', #args[1]}" +
                            "}"))
    public List<ExhibitionManageContact> listOfCache(ExhibitionManageContactDTO modelDTO, List<Long> ids) {
        //查询
        QueryWrapper<ExhibitionManageContact> queryWrapper = new QueryWrapper<>();
        if (modelDTO != null) {
            queryWrapper.lambda().eq(modelDTO.getExhibitionManageId() != null, ExhibitionManageContact::getExhibitionManageId, modelDTO.getExhibitionManageId());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getContact()), ExhibitionManageContact::getContact, modelDTO.getContact());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getContactMobile()), ExhibitionManageContact::getContactMobile, modelDTO.getContactMobile());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getEmail()), ExhibitionManageContact::getEmail, modelDTO.getEmail());
            queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getSpaceNames()), ExhibitionManageContact::getSpaceNames, modelDTO.getSpaceNames());
        }
        if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(ExhibitionManageContact::getId, ids);
        }
        queryWrapper.lambda().eq(ExhibitionManageContact::getIsDeleted, BusinessConstant.DELETE_NO);
        List<ExhibitionManageContact> modelList = list(queryWrapper);
        return modelList;
    }

    @Override
    public Boolean updateExhibitionContact(ExhibitionManageContactModifyDTO params) {
        Long exhibitionManageId = params.getExhibitionManageId();
        // 先删除历史记录
        update(new UpdateWrapper<ExhibitionManageContact>().lambda()
                .eq(ExhibitionManageContact::getExhibitionManageId,exhibitionManageId)
                .eq(ExhibitionManageContact::getIsDeleted, BusinessConstant.NO)
                .set(ExhibitionManageContact::getIsDeleted,BusinessConstant.YES));
        List<ExhibitionManageContactDTO> exhibitionManageContactList = params.getExhibitionManageContactList();
        for (ExhibitionManageContactDTO exhibitionManageContactDTO : exhibitionManageContactList) {
            exhibitionManageContactDTO.setExhibitionManageId(exhibitionManageId);
            ExhibitionManageContact exhibitionManageContact = BeanUtil.copyProperties(exhibitionManageContactDTO, ExhibitionManageContact.class, CommonConstants.COMMON_IGNORE_PROPERTIES);
            save(exhibitionManageContact);
        }
        return true;
    }

    @Override
    public List<ExhibitionManageContactVO> queryExhibitionContact(ExhibitionManageContactPageQuery params) {
        List<ExhibitionManageContact> list = list(new QueryWrapper<ExhibitionManageContact>().lambda()
                .eq(ExhibitionManageContact::getExhibitionManageId, params.getExhibitionManageId())
                .eq(ExhibitionManageContact::getIsDeleted, BusinessConstant.NO));
        return ConvertUtil.convertList(list,ExhibitionManageContactVO.class);
    }


    /**
     * do转vo
     *
     * @param model model
     * @return modelVo
     */
    @Override
    public ExhibitionManageContactVO doToVo(ExhibitionManageContact model) {
        ExhibitionManageContactVO modelVo = new ExhibitionManageContactVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }

    /**
     * do转vo
     *
     * @param modelList modelList
     * @return list
     */
    @Override
    public List<ExhibitionManageContactVO> doToVos(List<ExhibitionManageContact> modelList) {
        if (modelList == null || modelList.isEmpty()) {
            return null;
        }
        List<ExhibitionManageContactVO> result = new ArrayList<>();
        for (ExhibitionManageContact model : modelList) {
            result.add(doToVo(model));
        }
        return result;
    }
}
